package com.carter.springframework.beans.factory.support;

import com.carter.springframework.beans.exception.BeanException;
import com.carter.springframework.beans.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

import java.lang.reflect.Constructor;

/**
 * cglib实例化bean策略实现类
 */
public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy{
    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor<?> constructor, Object[] args) throws BeanException {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(beanDefinition.getBeanClass());
        enhancer.setCallback(new NoOp() {
        });
        if (constructor==null){
            return enhancer.create();
        }else {
            return enhancer.create(constructor.getParameterTypes(),args);
        }
    }
}
